<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>复兴豆芽厂每日销售情况</title>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="./jquery.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <style>
        #levelDiv {
            position: absolute;
            top: 180px;
            right: 30%;

        }

        .smart-green {
            margin-left: auto;
            margin-right: auto;
            max-width: 700px;
            background: #F8F8F8;

            font: 12px Arial, Helvetica, sans-serif;
            color: #666;
            border-radius: 5px;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
        }

        .smart-green h1 {
            font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
            padding: 20px 0px 20px 40px;
            display: block;
            margin: -30px -30px 10px -30px;
            color: #FFF;
            background: #9DC45F;
            text-shadow: 1px 1px 1px #949494;
            border-radius: 5px 5px 0px 0px;
            -webkit-border-radius: 5px 5px 0px 0px;
            -moz-border-radius: 5px 5px 0px 0px;
            border-bottom: 1px solid #89AF4C;
        }

        .smart-green h1 > span {
            display: block;
            font-size: 11px;
            color: #FFF;
        }

        .smart-green label {
            display: block;
            margin: 0px 0px 2px;
        }

        .smart-green label > span {
            float: left;
            margin-top: 5px;
            color: #5E5E5E;
            width: 10%;
        }

        .smart-green input[type="text"], .smart-green input[type="email"], .smart-green textarea, .smart-green select {
            color: #555;
            height: 30px;
            line-height: 15px;
            width: 10%;
            padding: 0px 0px 0px 10px;
            margin-left: 20px;
            border: 1px solid #E5E5E5;
            background: #FBFBFB;
            font: normal 14px/14px Arial, Helvetica, sans-serif;
        }

        .smart-green textarea {
            height: 100px;
            padding-top: 10px;
        }

        .smart-green select {
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            text-indent: 0.01px;
            text-overflow: '';
            width: 25%;
            height: 30px;
        }

        .smart-green .button {
            background-color: #9DC45F;
            border-radius: 5px;
            -webkit-border-radius: 5px;
            -moz-border-border-radius: 5px;
            border: none;
            padding: 10px 10px 10px 10px;
            color: #FFF;
            text-shadow: 1px 1px 1px #949494;
            width: 70px;
            text-align: center;
        }

        .smart-green .button:hover {
            background-color: #80A24A;
        }
        table {
            border-collapse: separate;
        }
        tr {
            height: 40px;
        }
        td{
            width: 40px;
            text-align: center;
        }
        th{
            width: 40px;
            text-align: center;
        }
        input{
            width: 40px;
        }
        .longtd{
            width: 60px;
        }
        .disabled-input {
            background-color: #F8F8F8;
            border: none;
        }
        #dateInput{
            font-size: larger;
            width: 300px;
            text-align: center;
        }.ui-datepicker {
             background-color: #fff;
             border: 1px solid #ced4da;
             border-radius: 0.25rem;
         }
        .ui-datepicker table {
            font-size: 0.875rem;
        }
        .ui-datepicker-header {
            background-color: #007bff;
            color: #fff;
        }


    </style>





    <script>
        $(function () {
            $("#dateInput").datepicker({dateFormat: 'yy-mm-dd',dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
                dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
                monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                prevText: "上月",
                nextText: "下月",
                closeText: "关闭"});
            var currentDate = new Date();
            var year = currentDate.getFullYear();
            var month = ('0' + (currentDate.getMonth() + 1)).slice(-2);
            var day = ('0' + currentDate.getDate()).slice(-2);
            $('#dateInput').val(year + '-' + month + '-' + day);

            $("#dateInput").on('change keyup', function () {
                $('#myTbody').empty();
                changeValue();
            });
            $('#goStat').click(function () {
                window.location.href = '/statistics.html';
            });
            changeValue();

            function changeValue(){
                var dateStr = $("#dateInput").val();
                $.ajax({
                    url: "getOrderByDate?dateStr="+dateStr, dataType: "json", success: function (result) {
                        var list = result.orders;
                        for (let i = 0; i < list.length; i++) {
                            $("#dataTable tbody").append("<tr>" +
                                "<td><input type='hidden' name='orders["+i+"].orderId' value="+list[i].orderId+">"+list[i].salesmanName+"</td>" +
                                "<td><input name='orders["+i+"].carWeight' value="+list[i].carWeight+"></td>" +
                                "<td><input name='orders["+i+"].grossWeight' value="+list[i].grossWeight+"></td>" +
                                "<td><input name='orders["+i+"].packageWeight' value="+list[i].packageWeight+"></td>" +
                                "<td><input name='orders["+i+"].litterBeanSprouts' value="+list[i].litterBeanSprouts+"></td>" +
                                "<td><input id='order"+list[i].orderId+"_beanSprouts' class='disabled-input' name='orders["+i+"].beanSprouts' readonly='readonly'  value="+list[i].beanSprouts+"></></td>" +
                                "<td><input id='order"+list[i].orderId+"_totalPrice' class='disabled-input' name='orders["+i+"].totalPrice' readonly='readonly' value="+list[i].totalPrice+"></td>" +
                                "</tr>");
                        }
                    }
                });
            }

            $("#myForm").submit(function(event) {
                // 阻止表单默认提交行为
                event.preventDefault();
                // 获取表单数据
                //var formData = $("#myForm").serializeArray();
                //var jsonObject = JSON.parse('{"' + decodeURI(jsonString.replace(/&/g, '","').replace(/=/g, '":"')) + '"}');
                var formData = $(this).serializeArray();
                var orders = [];
                var currentOrderIndex = null;
                var currentOrder = null;
                formData.forEach(function (input) {
                    var nameParts = input.name.match(/orders\[(\d+)\]\.(.*)/);
                    if (nameParts) {
                        var index = parseInt(nameParts[1]);
                        var key = nameParts[2];
                        if (currentOrderIndex!== index) {
                            currentOrderIndex = index;
                            currentOrder = {};
                            orders.push(currentOrder);
                        }
                        currentOrder[key] = input.value;
                    }
                });
                for (var i in currentOrder) {
                    orders.push(currentOrder[i]);
                }
                // 发起POST请求提交表单数据
                $.ajax({
                    type: "POST",
                    url: "/calculateOrder",
                    data: JSON.stringify(orders),
                    contentType: "application/json",
                    success: function(response) {
                        $.each(response.orders, function (index, item) {
                            var orderId = item.orderId;
                            $("#order"+orderId+"_beanSprouts").val(item.beanSprouts);
                            $("#order"+orderId+"_totalPrice").val(item.totalPrice);
                        });
                    },
                    error: function(error) {
                        // 处理提交失败的情况
                        console.log("提交失败");
                    }
                });
            });

        });


    </script>
<body>
<form id="myForm" action="/calculateOrder" enctype="application/json" method="post" class="smart-green">
    <h1 id="title">
        <input class="button"  id="dateInput" value="切换日期" />
    </h1>
    <table id="dataTable">
        <thead>
        <tr><th>姓名</th><th>车重</th><th>毛重</th><th>打包</th><th>芽了</th><th class="longtd">豆芽重量</th><th>总价</th></tr>
        </thead>
        <tbody id="myTbody">
        </tbody>

    </table>
    <label>
        <span> </span>
        <input id="submitBut" type="submit" class="button" value="入库" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="goStat" type="submit" class="button" value="查看" />
    </label>
</form>

<!--<div id="scoreDiv">您的工作性价比：<span id="score"></span></div>-->

<div id="levelDiv"><span id="level"></span></div>
</body>
</html>